package com.xuetao.spider.task;

import com.xuetao.spider.domain.UserProfile;
import com.xuetao.spider.util.ApiConnector;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

/**
 * @Description
 * @Author xuetao wang
 * @Date 2021/3/31
 */
public class UserProfileTask extends BaseTask {

    private static final Document document;

    static {
        String html = ApiConnector.get("https://www.xiaohongshu.com/user/profile/58c4fcde6a6a691cfb4087bb");
        document = Jsoup.parse(html);
    }

    @Override
    public void doTask() {
        UserProfile userProfile = new UserProfile();

        userProfile.setNickName(getNickName());
        userProfile.setLocation(getLocation());
        userProfile.setUserBrief(getBrief());
        userProfile.setFollowNumber(getFollowNumber());
        userProfile.setFansNumber(getFansNumber());
        userProfile.setLikesNumber(getLikesNumber());

        logger.info("user info -> {}", userProfile);
    }

    public String getNickName() {
        String nickName = document.select("div.user-name")
                .select("span.name-detail").first().text();

        return nickName;
    }

    public String getLocation() {
        String location = document.select("span.location-text").text();
        return location;
    }

    public String getBrief() {
        String brief = document.select("div.user-brief").text();
        return brief;
    }

    public String getFollowNumber() {
        String followNumber = document.select("div.card-info")
                .select("div.info").first().
                        select("span.info-number").text();
        return followNumber;
    }

    public String getFansNumber() {
        String fansNumber = document.select("div.card-info")
                .select("div.info").eq(1).
                        select("span.info-number").text();
        return fansNumber;
    }

    public String getLikesNumber() {
        String likesNumber = document.select("div.card-info")
                .select("div.info").eq(2).
                        select("span.info-number").text();
        return likesNumber;
    }
}
